var TOOL = {
	Host: 'http://106.112.128.46:8081/smartoilet/',
	// Host:"http://192.168.1.41:8081/smartoilet/",
	phone_test: /^1[3|4|5|6|7|8|9][0-9]{9}$/,
	toast: function(text, time) {
		$("body").append("<span class='toast-box'>" + text + "</span>");
		$(".toast-box").css({
			"font-size": "14px",
			"letter-spacing": "1px",
			"white-space": "nowrap",
			"padding": "6px 16px 6px 30px",
			"text-align": "center",
			"background": "#000",
			"color": "#fff",
			"opacity": ".7",
			"border-radius": "6px",
			"position": "fixed",
			"top": "80%",
			"left": "50%",
			"transform": "translate(-50%, -50%)",
			"z-index": "200000"
		});
		setTimeout(function() {
			$(".toast-box").remove();
		}, time)
	},
	setCookie: function(cname, cvalue, days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "expires=" + date.toGMTString();
		document.cookie = cname + "=" + cvalue + "; " + expires;
	},
	getCookie: function(cname) {
		var name = cname + "=";
		var ca = document.cookie.split(';');
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i].trim();
			if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
		}
		return "";
	},
	getUrlString: function(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"),
			r = window.location.search.substr(1).match(reg);
		if (r != null) return unescape(r[2]);
		return null;
	},
	//日期大小判断
	compareDate: function(d1, d2) {
		var arrayD1 = d1.val().split("-");
		var date1 = arrayD1[0] + arrayD1[1] + arrayD1[2];
		var arrayD2 = d2.val().split("-");
		var date2 = arrayD2[0] + arrayD2[1] + arrayD2[2];
		if (date1 >= date2) {
			return false;
		} else {
			return true;
		}
	},
	//日期转换
	DT: function(timestamp, formats) {
		if (timestamp == 0 || timestamp == "0") return '';
		formats = formats || 'Y-m-d H:i:s';
		var zero = function(value) {
			if (value < 10) {
				return '0' + value;
			}
			return value;
		};
		var myDate = timestamp ? new Date(timestamp * 1000) : new Date();
		var year = myDate.getFullYear();
		var month = zero(myDate.getMonth() + 1);
		var day = zero(myDate.getDate());
		var hour = zero(myDate.getHours());
		var minite = zero(myDate.getMinutes());
		var second = zero(myDate.getSeconds());
		return formats.replace(/Y|m|d|H|i|s/ig, function(matches) {
			return ({
				Y: year,
				m: month,
				d: day,
				H: hour,
				i: minite,
				s: second
			})[matches];
		});
	},
	//验证手机号
	phoneReg: function(phone, fun) {
		var Reg = /(^1[3|4|5|6|7|8]\d{9}$)|(^09\d{8}$)/;
		if (!Reg.test(phone) && phone != '') {
			fun()
		}
	},
	//邮箱验证
	emailReg: function(value, fun) {
		var reg = new RegExp("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$");
		if (!reg.test(value) && value != '') { //正则验证不通过，格式不对
			fun()
		}
	},
	//file图片上传显示
	imgLoad: function(input_file, img) {
		input_file.change(function() {
			var file = this.files[0];
			if (window.FileReader) {
				var reader = new FileReader();
				reader.readAsDataURL(file);
				//监听文件读取结束后事件    
				reader.onloadend = function(e) {
					// console.log(reader);
					// img.attr("src", e.target.result); //e.target.result就是最后的路径地址
					if (e.target.result != '') {
						img.attr("src", e.target.result);
					} else {
						img.attr("src", '');
					}
				};
			}
		});
	},
	//form表单文件上传
	submit: function(token,Type, Url, Data, ok) {
		$.ajax({
			type: Type,
			url: Url, //url
			beforeSend: function(xhr) {
				xhr.setRequestHeader("header", token);
			},
			data: Data,
			async: true,
			success: function(data) {
				ok(data)
			}
		})

	},
	//form表单带图片文件上传
	img_submit: function(token,Type, Url, Data, ok) {
		$.ajax({
			type: Type,
			contentType: false,
			processData: false,
			url: Url, //url
			beforeSend: function(xhr) {
				xhr.setRequestHeader("header", token);
			},
			data: Data,
			async: true,
			success: function(data) {
				ok(data)
			}
		})

	},
	setFire:function(page_url,event_name){
		mui.fire(plus.webview.getWebviewById(page_url), event_name)
	},
	getFire:function(event_name,fun){
		document.addEventListener(event_name, function() {
			fun()
		}, false)
	},
	open_window:function(url,id,extras){
		mui.openWindow({
			url: url,
			id: id,
			extras:extras,
			show: {
				autoShow: true, //页面loaded事件发生后自动显示，默认为true
				aniShow: "slide-in-right", //页面显示动画，默认为”slide-in-right“；
			},
			waiting:{
				autoShow:false
			}
		})
	},
	//等待加载
	waiting:function(){
		plus.nativeUI.showWaiting( "" ,{"background":"rgba（0,0,0,0)","color":"#000","style":"black","model":"true",});
	},
	//停止加载
	close_waiting:function(){
		plus.nativeUI.closeWaiting();
	},
	get_date:function(){
		function getNow(s) {
			return s < 10 ? '0' + s: s;
		}

		var myDate = new Date();
		//获取当前年
		var year=myDate.getFullYear();
		//获取当前月
		var month=myDate.getMonth()+1;
		//获取当前日
		var date=myDate.getDate(); 
		var h=myDate.getHours();       //获取当前小时数(0-23)
		var m=myDate.getMinutes();     //获取当前分钟数(0-59)
		var s=myDate.getSeconds();  

		// var now=year+'-'+getNow(month)+"-"+getNow(date)+" "+getNow(h)+':'+getNow(m)+":"+getNow(s);
		var now=getNow(h)+':'+getNow(m)+":"+getNow(s);
		return now;
	},
	post:function(url,data,ok){
		$.post(TOOL.Host + url,{data:JSON.stringify(data)},function(res){
			ok(res)
		},'json')
	},
	//获取经纬度
	location:function(ok){
		plus.geolocation.getCurrentPosition(function(p) {
			// console.log('维度:' + p.coords.latitude + '\经度:' + p.coords.longitude + '\海拔:' + p.coords.altitude);	
			ok(p.coords.longitude,p.coords.latitude)
		}, function(e) {
			mui.toast(e.message+'，请打开位置权限！');
		}, {
			provider: 'baidu'
		});
	}
	

};
